#include "control.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "cJSON.h"
#include "freertos.h"
#include "stm32f4xx_hal.h"
#include "timers.h"
#include "semphr.h"
#include "main.h"
#include "link.h"
extern uint8_t menu;
extern info ttinfo;
extern uint8_t buzzerflag;
extern TimerHandle_t  buzzertimerhandle;
extern SemaphoreHandle_t semhandle;
void control_light(cJSON*obj)
{

  cJSON*whichobj=cJSON_GetObjectItem(obj,"which");
	if(!whichobj)
		return ;
  cJSON*statobj=cJSON_GetObjectItem(obj,"stat");
	if(!statobj)
		return  ;
	int which =whichobj->valueint;
	int stat=statobj->valueint;
	
  if(which<=0||which>4||stat<0||stat>1)
     return ;
   switch(which)
	 {
	     case 1:
			 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,(stat==0) ? GPIO_PIN_RESET : GPIO_PIN_SET);
		   break;
		   case 2:
			 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,(stat==0) ? GPIO_PIN_RESET : GPIO_PIN_SET);
		   break;
			 case 3:
			 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,(stat==0) ? GPIO_PIN_RESET : GPIO_PIN_SET);
		   break;
			  case 4:
			 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,(stat==0) ? GPIO_PIN_RESET : GPIO_PIN_SET);
		   break;

	 }


}
void  control_menu(cJSON*obj)
{
  cJSON* tabobj=cJSON_GetObjectItem(obj,"tab");
	if(tabobj)
	{
	    if(!strcmp(tabobj->valuestring,"1"))
			{
			   menu=1;
			
			}
			else if(!strcmp(tabobj->valuestring,"2"))
			{
			
			   menu=2;
			
			}
	    	else if(!strcmp(tabobj->valuestring,"3"))
			{
			
			   menu=3;
			
			}
	
	
	}





}
void buzzer_start()
{

   xTimerStart(buzzertimerhandle,portMAX_DELAY);
	 buzzerflag=1;

}
void buzzer_stop()
{
   xTimerStop(buzzertimerhandle,portMAX_DELAY);
   HAL_GPIO_WritePin(GPIOA,GPIO_PIN_8,GPIO_PIN_RESET); 
	 buzzerflag=0;


}
void control_buzzer(cJSON*obj)
{

  cJSON*buzzerobj=cJSON_GetObjectItem(obj,"stat");
	if(!buzzerobj)
		return ;
	int stat=buzzerobj->valueint;
	if(stat==1)
	{
	  
	   buzzer_start();
	
	}
	else if(stat==0)
	{
	  buzzer_stop();
	
	
	}
		




}
void  control_time(cJSON*obj)
{
    cJSON* timeobj=cJSON_GetObjectItem(obj,"date");
	   if(!timeobj)
			 return ;
		 int time=timeobj->valueint;
		 
		 xSemaphoreTake(semhandle,portMAX_DELAY);
	    ttinfo.time=time;
		 printf("ok");
     xSemaphoreGive(semhandle);
		
}
void  control_alarm(cJSON*obj)
{
    cJSON* hourobj=cJSON_GetObjectItem(obj,"hour");
	  cJSON* minobj=cJSON_GetObjectItem(obj,"min");
	  if(!hourobj||!minobj)
	      return ;
		uint8_t hour=hourobj->valueint;
		uint8_t min=minobj->valueint;
		
	
	  Add_alarm(hour,min);
	
}
void analysis_control(char* buffer)
{
  int len=0;
	
  char*p=buffer;
	char*p1=NULL;
	char*p2=NULL;
	char str[8]={0};
	char content[256]={0};
	int comma=0;
	while(*p!='\0')
	{
	  if(*p==',')
		{comma++;}
		if(comma==2&&*p==',')
		{
		   p1=p+1;
		
		
		}
			if(comma==3&&*p==',')
		{
		   p2=p+1;
		
		
		}
		
	   p++;

	}
   
    memcpy(str,p1,p2-p1);
	  len=atoi(str);
	  printf("       len:%d\r\n",len);
	  memcpy(content,p2,len);
	  printf("        content:%s\r\n",content);
	
	
    cJSON*jsonobj=cJSON_Parse(content); //"{\"cmd\":\"menu\",\"tab\":\"2\"}"

	  if(jsonobj==NULL)
		{
		  cJSON_Delete(jsonobj);
		  return ;
		}
		cJSON*cmdobj=cJSON_GetObjectItem(jsonobj,"cmd");
		if(!strcmp(cmdobj->valuestring,"menu"))
		{
		   control_menu(jsonobj);
		
		
		}
		else if(!strcmp(cmdobj->valuestring,"light"))
		{
		    control_light(jsonobj);
		
		
		}
		else if(!strcmp(cmdobj->valuestring,"buzzer"))
		{
		    control_buzzer(jsonobj);
		
		
		}
			else if(!strcmp(cmdobj->valuestring,"time"))
		{
		    control_time(jsonobj);
		
		
		}
		
				else if(!strcmp(cmdobj->valuestring,"alarm"))
		{
		    control_alarm(jsonobj);
		
		
		}
		 cJSON_Delete(jsonobj);
		
	

}
